package net.sunofbeach.blog.services.impl;

import lombok.extern.slf4j.Slf4j;
import net.sunofbeach.blog.dao.LyReplyDao;
import net.sunofbeach.blog.pojo.LyReply;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.ILyReplyService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

@Slf4j
@Service
@Transactional
public class LyReplyServiceImpl  extends BaseService implements ILyReplyService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private LyReplyDao lyReplyDao;
    @Autowired
    private TaskService taskService;
    @Override
    public ResponseResult postLyReply(LyReply lyReply) {
        String nickname = lyReply.getNickname();
        if (TextUtils.isEmpty(nickname)) {
            return ResponseResult.FAILED("留言昵称不可以为空");
        }
        String email = lyReply.getEmail();
        if (TextUtils.isEmpty(email)) {
            return ResponseResult.FAILED("留言邮箱不可以为空");
        }
        String content = lyReply.getContent();
        if (TextUtils.isEmpty(content)) {
            return ResponseResult.FAILED("留言内容不可以为空");
        }
        String parentId = lyReply.getParentId();
        if (TextUtils.isEmpty(parentId)) {
            return ResponseResult.FAILED("父级留言ID不可以为空");
        }
        String parentName = lyReply.getParentName();

        if (TextUtils.isEmpty(parentName)) {
            return ResponseResult.FAILED("被回复者不可以为空");
        }
        String orderUrl = lyReply.getOrderUrl();
        if ((!TextUtils.isEmpty(orderUrl)) && (!orderUrl.contains("http"))) {
            return ResponseResult.FAILED("网站URL需要包含有https或者http的全域名");
        }
        String parentEmail = lyReply.getParentEmail();
        if (!TextUtils.isEmpty(parentEmail)) {
            try {
                taskService.sendCommentNotify(nickname,content,parentEmail);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String avatar = lyReply.getAvatar();
        if (TextUtils.isEmpty(avatar)) {
            lyReply.setAvatar(Constants.User.DEFAULT_AVATAR);
        }
        lyReply.setState("1");
        lyReply.setId(idWorker.nextId() + "");

        lyReply.setCreateTime(new Date());
        lyReplyDao.save(lyReply);
        return ResponseResult.SUCCESS("留言成功");
    }

    @Override
    public ResponseResult listLysReply(int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        //没有就往下走
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<LyReply> all =lyReplyDao.findAll(pageable);
        return ResponseResult.SUCCESS("获取留言列表成功").setData(all);
    }

    @Override
    public ResponseResult getLyReplyCount() {
        long count = lyReplyDao.count();
        return ResponseResult.SUCCESS("子评论总数获取成功").setData(count);
    }
}

